home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 January: Mac OS SDK / Dev.CD Jan 97 SDK2.toast / Development Kits (Disc 2) / OpenDoc / OpenDoc Development / Debugging Support / OpenDoc™ Source Code / Utilities / SIHshTbl.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-08-28  |  6.8 KB  |  250 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        SIHshTbl.cpp
  3.  
  4.     Contains:    xxx put contents here xxx
  5.  
  6.     Owned by:    Jens Alfke
  7.  
  8.     Copyright:    © 1996 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     Change History (most recent first):
  11.  
  12.          <2>      5/1/96    JA        Changed kMaxValueSize to 12 'cause some
  13.                                     structs get larger under PPC alignment.
  14.                                     [1213332]
  15.  
  16.     To Do:
  17. */
  18.  
  19. /*
  20.     File:        SIHshTbl.cpp
  21.  
  22.     Contains:    Implementation of SIHashTable class
  23.  
  24.     Owned by:    Nick Pilch
  25.  
  26.     Copyright:    © 1993 - 1995 by Apple Computer, Inc., all rights reserved.
  27.  
  28.     In Progress:
  29.         
  30. */
  31.  
  32. #ifndef _SIHSHTBL_
  33. #include "SIHshTbl.h"
  34. #endif
  35.  
  36. #ifndef _HshTbl_
  37. #include "HshTbl.h"
  38. #endif
  39.  
  40. #ifndef _EXCEPT_
  41. #include "Except.h"
  42. #endif
  43.  
  44. #ifndef _ODTYPES_
  45. #include "ODTypes.h"
  46. #endif
  47.  
  48. #ifndef _UTILERRS_
  49. #include "UtilErrs.h"
  50. #endif
  51.  
  52. #ifndef __AEOBJECTS__
  53. #include <AEObjects.h>
  54. #endif
  55.  
  56. #ifndef _ODDEBUG_
  57. #include "ODDebug.h"
  58. #endif
  59.  
  60. #pragma segment SIHashTable
  61.  
  62. //==============================================================================
  63. // Implementation comments
  64. //==============================================================================
  65.  
  66. /*
  67. The error codes returned from Hash Manager functions are incompletely
  68. documented.
  69. Therefore, I make some pessimistic assumptions. If creating a new hash table or
  70. any other routine that may need to allocate memory fails, I assume that it's
  71. out of memory and throw kODErrOutOfMemory. Otherwise, I don't check error codes
  72. explicitly.
  73. */
  74.  
  75. #define kMaxValueSize 12        /* was 10 */
  76. //==============================================================================
  77. // SIHashTable
  78. //==============================================================================
  79.  
  80. //------------------------------------------------------------------------------
  81. // SIHashTable::SIHashTable
  82. //------------------------------------------------------------------------------
  83.  
  84. SIHashTable::SIHashTable()
  85. {
  86.     fSIHashTable = kODNULL;
  87.     fValueSize = 0 ;
  88. }
  89.  
  90. //------------------------------------------------------------------------------
  91. // SIHashTable::Initialize
  92. //------------------------------------------------------------------------------
  93.  
  94. void SIHashTable::Initialize(ODULong numEntries, ODUShort keySize,
  95.                                 ODUShort valueSize, ODBoolean inSysHeap)
  96. {
  97.     if ( valueSize > kMaxValueSize )
  98.         THROW( kODErrHashValueSizeTooBig );
  99.     OSErr error = NewHashTable(numEntries, keySize, valueSize,
  100.             (MemProcBlock*)NULL, inSysHeap, &fSIHashTable);
  101.     if (error || !fSIHashTable)
  102.         THROW(kODErrOutOfMemory);
  103.     fValueSize = valueSize ;
  104. }
  105.  
  106. //------------------------------------------------------------------------------
  107. // SIHashTable::~SIHashTable
  108. //------------------------------------------------------------------------------
  109.  
  110. SIHashTable::~SIHashTable()
  111. {
  112.     if (fSIHashTable)
  113.         DisposeHashTable(&fSIHashTable, NULL);
  114. }
  115.  
  116. //------------------------------------------------------------------------------
  117. // SIHashTable::ReplaceEntry
  118. //------------------------------------------------------------------------------
  119.  
  120. void SIHashTable::ReplaceEntry(ODKeyPtr key, ODEntryPtr value)
  121. {
  122.     OSErr error = ::ReplaceEntry(fSIHashTable, NULL, (KeyPtr)key, (HEntryPtr)value);
  123.     if (error)
  124.         THROW(kODErrOutOfMemory);
  125. }
  126.  
  127. //------------------------------------------------------------------------------
  128. // SIHashTable::RemoveEntry
  129. //------------------------------------------------------------------------------
  130.  
  131. void SIHashTable::RemoveEntry(ODKeyPtr key)
  132. {
  133.     // As far as I could tell, RemoveKeyEntry never returns an error.
  134.  
  135.     RemoveKeyEntry(fSIHashTable, NULL, (KeyPtr)key);
  136. }
  137.  
  138. //------------------------------------------------------------------------------
  139. // SIHashTable::GetValue
  140. //------------------------------------------------------------------------------
  141.  
  142. ODBoolean SIHashTable::GetValue(ODKeyPtr key, ODEntryPtr value)
  143. {
  144.     if (GetKeyValue(fSIHashTable, NULL, (KeyPtr)key, (HEntryPtr)value) != noErr)
  145.         return kODFalse;
  146.     else
  147.         return kODTrue;
  148. }
  149.  
  150. //------------------------------------------------------------------------------
  151. // SIHashTable::Exists
  152. //------------------------------------------------------------------------------
  153.  
  154. ODBoolean SIHashTable::Exists(ODKeyPtr key)
  155. {
  156.     char someScratchValueSpace[kMaxValueSize] ;
  157.     WASSERT(fValueSize <= kMaxValueSize);
  158. //    if ( fValueSize > kMaxValueSize )
  159. //        THROW( -1000 ) ;                // <eeh> need a real # here
  160.  
  161.     OSErr err = GetKeyValue( fSIHashTable, kODNULL, (KeyPtr)key,
  162.         (HEntryPtr)someScratchValueSpace ) ;
  163.     return err == noErr ;
  164. }
  165.  
  166. //------------------------------------------------------------------------------
  167. // SIHashTable::GetSIHashTable
  168. //------------------------------------------------------------------------------
  169.  
  170. HashTable SIHashTable::GetSIHashTable()
  171. {
  172.     return fSIHashTable;
  173. }
  174.  
  175. //==============================================================================
  176. // SIHashTableIterator
  177. //==============================================================================
  178.  
  179. //------------------------------------------------------------------------------
  180. // SIHashTableIterator::SIHashTableIterator
  181. //------------------------------------------------------------------------------
  182.  
  183. SIHashTableIterator::SIHashTableIterator(SIHashTable* table)
  184. {
  185.     fTable = table;
  186.     fIndex = 0;
  187.     fDone = kODFalse;
  188. }
  189.  
  190. //------------------------------------------------------------------------------
  191. // SIHashTableIterator::~SIHashTableIterator
  192. //------------------------------------------------------------------------------
  193.  
  194. SIHashTableIterator::~SIHashTableIterator()
  195. {
  196. }
  197.  
  198. //------------------------------------------------------------------------------
  199. // SIHashTableIterator::First
  200. //------------------------------------------------------------------------------
  201.  
  202. void SIHashTableIterator::First(ODKeyPtr key, ODEntryPtr value)
  203. {
  204.     if (!this->GetNext(key, value))
  205.         fDone = kODTrue;
  206. }
  207.  
  208. //------------------------------------------------------------------------------
  209. // SIHashTableIterator::Next
  210. //------------------------------------------------------------------------------
  211.  
  212. void SIHashTableIterator::Next(ODKeyPtr key, ODEntryPtr value)
  213. {
  214.     if (!this->GetNext(key, value))
  215.         fDone = kODTrue;
  216. }
  217.  
  218. //------------------------------------------------------------------------------
  219. // SIHashTableIterator::IsNotComplete
  220. //------------------------------------------------------------------------------
  221.  
  222. ODBoolean SIHashTableIterator::IsNotComplete()
  223. {
  224.     return !fDone;
  225. }
  226.  
  227. //------------------------------------------------------------------------------
  228. // SIHashTableIterator::GetNext
  229. //
  230. //    Returns true if another entry can be found, false otherwise.
  231. //------------------------------------------------------------------------------
  232.  
  233. ODBoolean SIHashTableIterator::GetNext(ODKeyPtr key, ODEntryPtr value)
  234. {
  235.     OSErr    result = noErr;
  236.     
  237.     do
  238.     {
  239.         result = GetIndexedEntry(fTable->GetSIHashTable(),
  240.                             kODNULL, fIndex, (KeyPtr)key, (HEntryPtr)value);
  241.         ++fIndex;
  242.         if (result == noErr)
  243.             return kODTrue;
  244.     }
  245.     while (result != kAEErrEndOfTable);
  246.  
  247.     return kODFalse;
  248. }
  249.  
  250.